/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-02 -- Cliff Simpkins: Initial Version
 * 2007-08-26 -- Cliff Simpkins: Logic changes for DNN module
 * 2007-09-02 -- Cliff Simpkins: Updated roster for guild rank labels
 * 2007-09-23 -- Cliff Simpkins: Added RepGroups DD and added Error message
 *                 to the exception logging stored procedure call
 * 2007-12-26 -- Cliff Simpkins: Adding Delete Error and Data Load calls
 **********************************************************************
 */
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using DotNetWarcraft.Common;
using DotNetWarcraft.WoWGuildRoster.Business;
using Microsoft.ApplicationBlocks.Data;

namespace DotNetWarcraft.WoWGuildRoster.Data {
  public class SqlDataProvider : DataProvider {

    protected const string providerType = "data";

    #region Private Members
    protected string _providerPath;
    protected string _objectQualifier;
    protected string _databaseOwner;
    #endregion
    #region Properties
    public string ProviderPath {
      get {
        return _providerPath;
      }
      set {
        _providerPath = value;
      }
    }
    public string ObjectQualifier {
      get {
        return _objectQualifier;
      }
      set {
        _objectQualifier = value;
      }
    }
    public string DatabaseOwner {
      get {
        return _databaseOwner;
      }
      set {
        _databaseOwner = value;
      }
    }
    #endregion

    #region Constructors
    public SqlDataProvider() {
      _connectionString = "";
      _providerPath = "";
      _objectQualifier = "";
      _databaseOwner = "";

      _connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];

      if (_objectQualifier != "" && (_objectQualifier.EndsWith("_") == false)) {
        _objectQualifier += "_";
      }

      if (_databaseOwner != "" && (_databaseOwner.EndsWith(".") == false)) {
        _databaseOwner += ".";
      }
    }
    #endregion

    #region WoWGuildRoster Methods - Errors
    public override List<GuildError> GetErrors() {
      List<GuildError> _errs = new List<GuildError>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetErrors");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetErrors - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no errors to process
        } else {
          //Okay, let's get to work - we have a table and errors to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildError _e = new GuildError(_dr);
            _errs.Add(_e);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _errs;
    }
    public override GuildError GetError(int ErrorId) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetError_ByErrorID", ErrorId);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetError - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetError - No Rows Returned");
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.WGRGeneralError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.GetError - Multiple Rows Returned"),
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetError - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildError(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and error to read
          return new GuildError(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetError")).WriteExceptionToDB();
        return null;
      }
    }

    public override bool DeleteErrors() {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteErrors");
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteErrors")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteErrors(int RosterId) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteErrors_ByRoster", RosterId);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteErrors(" + RosterId.ToString() + ")")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteError(int ErrorId) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteErrors_ByError", ErrorId);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteError(" + ErrorId.ToString() + ")")).WriteExceptionToDB();
        return false;
      }
    }

    #endregion

    #region WoWGuildRoster Methods - Rosters
    public override DataSet GetRostersDS() {
      DataSet _ds = new DataSet();

      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetRosterData");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster - No Tables Returned");
        } else {
          //If there is work to do, we do it here

        } 
      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _ds;

    }
    public override List<GuildRoster> GetRosters() {
      List<GuildRoster> _guilds = new List<GuildRoster>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetRosters");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildRoster _guild = new GuildRoster(_dr);
            _guilds.Add(_guild);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _guilds;
    }
    public override GuildRoster GetRoster(int rosterID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetRoster_ByRosterID", rosterID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster - No Rows Returned");
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildRosterError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.GetRoster - Multiple Rows Returned"), 
            -1, rosterID,
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildRoster(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildRoster(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, rosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetRoster")).WriteExceptionToDB();
        return null;
      }
    }

    public override int AddRoster(GuildRoster objGuildRoster) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        return (int)SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddRoster",
          objGuildRoster.Guild_Name,
          objGuildRoster.Guild_Server,
          objGuildRoster.Guild_Region,
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank0),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank1),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank2),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank3),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank4),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank5),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank6),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank7),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank8),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank9),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank10),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Guild_Alts_To),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Guild_Graphic),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Load_Date),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Load_ID),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Update_Date),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Update_By));
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.AddRoster(" + objGuildRoster.Guild_Name + ")")).WriteExceptionToDB();
        return 0;
      }
    }
    public override bool UpdateRoster(GuildRoster objGuildRoster) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_UpdateRoster",
          objGuildRoster.Roster_ID,
          objGuildRoster.Guild_Name,
          objGuildRoster.Guild_Server,
          objGuildRoster.Guild_Region,
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank0),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank1),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank2),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank3),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank4),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank5),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank6),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank7),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank8),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank9),
          SqlDataUtils.ValidateDbValue(objGuildRoster.GuildRank10),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Guild_Alts_To),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Guild_Graphic),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Load_Date),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Load_ID),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Update_Date),
          SqlDataUtils.ValidateDbValue(objGuildRoster.Last_Update_By));
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, objGuildRoster.Roster_ID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.UpdateRoster")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteRoster(int rosterID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteRoster",
          rosterID);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, rosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteRoster")).WriteExceptionToDB();
        return false;
      }
    }

    public override void ProcessGuildEntries_MarkRosterUnprocessed(int RosterID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteRosterModule_ByRoster",
          RosterID);
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, RosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.ProcessGuildEntries_MarkRosterUnprocessed(" + RosterID + ")")).WriteExceptionToDB();
      }
    }
    public override void ProcessGuildEntries_MarkPriorUnprocessed(int RosterID, DateTime LoadTimeStart) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_ProcessCharEntries_ClearProcessed_ByTimestamp",
          RosterID, LoadTimeStart);
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, RosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.ProcessGuildEntries_MarkPriorUnprocessed(" + RosterID + ", " + LoadTimeStart.ToString() + ")")).WriteExceptionToDB();
      }
    }
    public override void ProcessGuildEntries_MarkPriorUnprocessed(int RosterID, int LoadID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_ProcessCharEntries_ClearProcessed_ByLoad",
          RosterID, LoadID);
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, RosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.ProcessGuildEntries_MarkPriorUnprocessed(" + RosterID + ", " + LoadID.ToString() + ")")).WriteExceptionToDB();
      }
    }

    public override void ProcessGuildEntries_MarkForReload(int RosterID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_ProcessCharEntries_MarkForReload",
          RosterID);
      } catch (Exception e) {
        (new Business.Exceptions.GuildRosterError(e, -1, RosterID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.ProcessGuildEntries_MarkForReload(" + RosterID.ToString() + ")")).WriteExceptionToDB();
      }
    }

    #endregion

    #region WoWGuildRoster Methods - Char Entries
    public override List<GuildEntry> GetCharEntries() {
      return this.GetCharEntries(true);
    }
    public override List<GuildEntry> GetCharEntries(bool currentEntriesOnly) {
      return this.GetCharEntries(currentEntriesOnly, -1, 1, 99);
    }
    protected List<GuildEntry> GetCharEntries(bool currentEntriesOnly, int RosterID, int LevelRange_Min, int LevelRange_Max) {
      String _storedProc;
      List<GuildEntry> _entries = new List<GuildEntry>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }

        DataSet _ds = null;
        if (RosterID < 0) {
          if (currentEntriesOnly) {
            _storedProc = "DNW_WGR_GetCharEntries_Current";
          } else {
            _storedProc = "DNW_WGR_GetCharEntries_All";
          }
          _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + _storedProc);
        } else {
          _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntries_ByRoster_RangeCurrent",
                                         RosterID, LevelRange_Max, LevelRange_Min);
        }

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntries - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntry _entry = new GuildEntry(_dr);
            _entries.Add(_entry);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _entries;
    }
    public override List<GuildEntry> GetCharEntries_Alts(int mainCharID) {
      List<GuildEntry> _entries = new List<GuildEntry>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }

        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntries_Alts", mainCharID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntries_Alts - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntry _entry = new GuildEntry(_dr);
            _entries.Add(_entry);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _entries;
    }
    public override List<GuildEntry> GetCharEntries(int RosterID) {
      return GetCharEntries(RosterID, 99, 1);
    }
    public override List<GuildEntry> GetCharEntries(int RosterID, int LevelRange_Min, int LevelRange_Max) {
      return GetCharEntries(true, RosterID, LevelRange_Min, LevelRange_Max);
    }
    public override GuildEntry GetCharEntry(int entryID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntry_ByEntryID", entryID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntry();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildCharEntryError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry - Too Many Rows Returned"),
            -1, -1, entryID,
            "DotNetWarcraft.WoWGuildRoster.Data.GetCharEntry - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry")).WriteExceptionToDB();
        return null;
      }
    }
    public override GuildEntry GetCharEntry(String charName, String serverName) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntry_ByCharNameAndServer", charName, serverName);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntry();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildCharEntryError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.GetCharEntry - Too Many Rows Returned - Deleting duplicates"),
            -1, -1, -1,
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry(" + charName + ", " + serverName + ") - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          for (int d = 1; d < _ds.Tables[0].Rows.Count; d++) {
            this.DeleteCharEntry((new GuildEntry(_ds.Tables[0].Rows[d])).Char_ID);
          }
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildEntry(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryError(e, -1, -1, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntry(" + charName + ", " + serverName + ")")).WriteExceptionToDB();
        return null;
      }
    }
    public override int AddCharEntry(GuildEntry objCharEntry) {
      try {
        if (objCharEntry.Last_Update_Date == DateTime.MinValue) {
          objCharEntry.Last_Update_Date = DateTime.UtcNow;
        }
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        return (int)SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddCharEntry",
          SqlDataUtils.ValidateDbValue(objCharEntry.Roster_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Server),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Guild),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Region),
          SqlDataUtils.ValidateDbValue(objCharEntry.LoadStatus),
          SqlDataUtils.ValidateDbValue(objCharEntry.Current_Flag),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Name),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Race),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Gender),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Class),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Last_Login),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec_Active),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec_Primary),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_PrimaryTree),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree3),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_PrimaryTree),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree3),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof1_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof2_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Guild_Rank),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Guild_Notes),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Officer_Notes),
          SqlDataUtils.ValidateDbValue(objCharEntry.Primary_Char_Flag),
          SqlDataUtils.ValidateDbValue(objCharEntry.Alt_to_Main_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Load_Date),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Load_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Update_Date),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Update_By));
      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryError(e, objCharEntry.Last_Load_ID, objCharEntry.Roster_ID, objCharEntry.Char_ID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.AddCharEntry")).WriteExceptionToDB();
        return 0;
      }
    }
    public override bool UpdateCharEntry(GuildEntry objCharEntry) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_UpdateCharEntry",
          objCharEntry.Char_ID,
          SqlDataUtils.ValidateDbValue(objCharEntry.Roster_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Server),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Guild),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Region),
          SqlDataUtils.ValidateDbValue(objCharEntry.LoadStatus),
          SqlDataUtils.ValidateDbValue(objCharEntry.Current_Flag),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Name),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Race),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Gender),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Class),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Last_Login),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec_Active),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec_Primary),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_PrimaryTree),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec1_Tree3),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_PrimaryTree),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Spec2_Tree3),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof1),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof1_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof2),
          SqlDataUtils.ValidateDbValue(objCharEntry.Char_Prof2_Level),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Guild_Rank),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Guild_Notes),
          SqlDataUtils.ValidateDbValue(objCharEntry.Game_Officer_Notes),
          SqlDataUtils.ValidateDbValue(objCharEntry.Primary_Char_Flag),
          SqlDataUtils.ValidateDbValue(objCharEntry.Alt_to_Main_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Load_Date),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Load_ID),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Update_Date),
          SqlDataUtils.ValidateDbValue(objCharEntry.Last_Update_By));
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryError(e, objCharEntry.Last_Load_ID, objCharEntry.Roster_ID, objCharEntry.Char_ID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.UpdateCharEntry")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteCharEntry(int entryID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteCharEntry",
          entryID);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryError(e, -1, -1, entryID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteCharEntry")).WriteExceptionToDB();
        return false;
      }
    }
    #endregion

    #region WoWGuildRoster Methods - Char Entry Stats
    public override List<GuildEntryStat> GetCharEntryStats() {
      List<GuildEntryStat> _stats = new List<GuildEntryStat>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStats() - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntryStat _entryStat = new GuildEntryStat(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override List<GuildEntryStat> GetCharEntryStats_ByCharEntry(int entryID) {
      List<GuildEntryStat> _stats = new List<GuildEntryStat>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByCharEntry", entryID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStats_ByCharEntry(" + entryID.ToString() + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntryStat _entryStat = new GuildEntryStat(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override List<GuildEntryStat> GetCharEntryStats_ByCharEntryAndType(int entryID, WarcraftStatTypes statTypeEnum) {
      return GetCharEntryStats_ByCharEntryAndType(entryID, WGRConstants.WowStatType_Int(statTypeEnum));
    }
    public override List<GuildEntryStat> GetCharEntryStats_ByCharEntryAndType(int entryID, int statTypeID) {
      List<GuildEntryStat> _stats = new List<GuildEntryStat>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByCharEntryAndType", entryID, statTypeID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStats_ByCharEntryAndType(" + entryID.ToString() + ", " + statTypeID.ToString() + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntryStat _entryStat = new GuildEntryStat(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override List<GuildEntryStat> GetCharEntryStats_ByCharEntryAndTypeAndGroup(int entryID, WarcraftStatTypes statTypeEnum, String statGroup) {
      return GetCharEntryStats_ByCharEntryAndTypeAndGroup(entryID, WGRConstants.WowStatType_Int(statTypeEnum), statGroup);
    }
    public override List<GuildEntryStat> GetCharEntryStats_ByCharEntryAndTypeAndGroup(int entryID, int statTypeID, String statGroup) {
      List<GuildEntryStat> _stats = new List<GuildEntryStat>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByCharEntryAndTypeAndGroup", entryID, statTypeID, statGroup);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStats_ByCharEntryAndTypeAndGroup(" + entryID.ToString() + ", " + statTypeID.ToString() + ", " + statGroup + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            GuildEntryStat _entryStat = new GuildEntryStat(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override GuildEntryStat GetCharEntryStat(int entryStatID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStat", entryStatID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntryStat();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildCharEntryStatError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat - Too Many Rows Returned"),
            -1, -1, -1, entryStatID,
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat(" + entryStatID.ToString() + ") - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, -1, -1, -1, entryStatID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat(" + entryStatID.ToString() + ")")).WriteExceptionToDB();
        return new GuildEntryStat();
      }
    }
    public override GuildEntryStat GetCharEntryStat(int charID, int statType, String statGroup, String statEntry) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStat_ByValues", charID, statType, statGroup, statEntry);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no entry to process
          return new GuildEntryStat();
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildCharEntryStatError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat - Too Many Rows Returned"),
            -1, -1, charID, -1,
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat(" + charID.ToString() + ", " + statType.ToString() + ", " + statGroup + ", " + statEntry + ") - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new GuildEntryStat(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, -1, -1, charID, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStat(" + charID.ToString() + ", " + statType.ToString() + ", " + statGroup + ", " + statEntry + ")")).WriteExceptionToDB();
        return new GuildEntryStat();
      }
    }
    public override int AddUpdateCharEntryStat(GuildEntryStat objCharEntryStat) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        return (int)SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddUpdateCharEntryStat",
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Char_ID),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Type),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Group),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Entry),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Value_int),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Value_dec),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Stat_Value_str),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Last_Load_Date),
          SqlDataUtils.ValidateDbValue(objCharEntryStat.Last_Load_ID));
      } catch (System.NullReferenceException) {
        //Seems to be triggered when using DBNull.Value - So simply catching this here.
        return -1;
      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, objCharEntryStat.Last_Load_ID, -1, objCharEntryStat.Char_ID, objCharEntryStat.CharStat_ID, "DotNetWarcraft.WoWGuildRoster.Data.AddUpdateCharEntryStat")).WriteExceptionToDB();
        return -1;
      }
    }
    public override bool DeleteCharEntryStat(int entryStatID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteCharEntryStat",
          entryStatID);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, -1, -1, -1, entryStatID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteCharEntryStat")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteCharEntryStats_ByChar(int entryID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
          }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteCharEntryStats_ByCharEntry",
          entryID);
        if (update > 0) {
          return true;
          } else {
          return false;
          }
        } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, -1, -1, entryID, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteCharEntryStats_ByChar")).WriteExceptionToDB();
        return false;
        }
      }
    public override bool DeleteCharEntryStats_ByChar(int entryID, WarcraftStatTypes statTypeEnum) {
      return DeleteCharEntryStats_ByChar(entryID, WGRConstants.WowStatType_Int(statTypeEnum));
      }
    public override bool DeleteCharEntryStats_ByChar(int entryID, int statTypeID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
          }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteCharEntryStats_ByCharEntryAndType",
          entryID, statTypeID);
        if (update > 0) {
          return true;
          } else {
          return false;
          }
        } catch (Exception e) {
        (new Business.Exceptions.GuildCharEntryStatError(e, -1, -1, entryID, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteCharEntryStats_ByChar (TypeID=" + statTypeID.ToString() + ")")).WriteExceptionToDB();
        return false;
        }
      }

    public override List<QuickStatListItem> GetEntryStatsLIs_ByGuildRank(int guildRank, bool mainsOnly) {
      List<QuickStatListItem> _stats = new List<QuickStatListItem>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByGuildRank", guildRank, (mainsOnly ? 1 : 0));

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetEntryStatsLIs_ByGuildRank(" + guildRank.ToString() + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rows to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            QuickStatListItem _entryStat = new QuickStatListItem(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override List<QuickStatListItem> GetEntryStatsLIs_ByStatEntryAndType(String statEntry, WarcraftStatTypes statTypeEnum, bool mainsOnly) {
      return GetEntryStatsLIs_ByStatEntryAndType(statEntry, WGRConstants.WowStatType_Int(statTypeEnum), mainsOnly);
    }
    public override List<QuickStatListItem> GetEntryStatsLIs_ByStatEntryAndType(String statEntry, int statTypeID, bool mainsOnly) {
      List<QuickStatListItem> _stats = new List<QuickStatListItem>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByStatEntryAndType", statEntry, statTypeID, (mainsOnly ? 1 : 0));

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetEntryStatsLIs_ByStatEntryAndType(" + statEntry + ", " + statTypeID.ToString() + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            QuickStatListItem _entryStat = new QuickStatListItem(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }
    public override List<QuickStatListItem> GetEntryStatsLIs_ByStatEntryAndTypeAndGroup(String statEntry, String statGroup, WarcraftStatTypes statTypeEnum, bool mainsOnly) {
      return GetEntryStatsLIs_ByStatEntryAndTypeAndGroup(statEntry, statGroup, WGRConstants.WowStatType_Int(statTypeEnum), mainsOnly);
    }
    public override List<QuickStatListItem> GetEntryStatsLIs_ByStatEntryAndTypeAndGroup(String statEntry, String statGroup, int statTypeID, bool mainsOnly) {
      List<QuickStatListItem> _stats = new List<QuickStatListItem>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetCharEntryStats_ByStat", statEntry, statGroup, statTypeID, (mainsOnly ? 1 : 0));

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetCharEntryStats_ByStatEntryAndTypeAndGroup(" + statEntry + ", " + statGroup + ", " + statTypeID.ToString() + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            QuickStatListItem _entryStat = new QuickStatListItem(_dr);
            _stats.Add(_entryStat);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _stats;
    }


    #endregion

    #region WowGuildRoster Methods - Data Loads
    public override List<DataLoad> GetDataLoads() {
      List<DataLoad> _loads = new List<DataLoad>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetDataLoads");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoads() - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
        } else {
          //Okay, let's get to work - we have a table and rosters to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            DataLoad _dl = new DataLoad(_dr);
            _loads.Add(_dl);
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _loads;
    }
    public override DataLoad GetDataLoad(int loadID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetDataLoad", loadID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad - No Rows Returned");
        } else if (_ds.Tables[0].Rows.Count > 1) {
          //Too many rows returned; uh-oh!
          (new Business.Exceptions.GuildLoadError(new Exception(
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad - Too Many Rows Returned"),
            loadID,
            "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad - " + _ds.Tables[0].Rows.Count.ToString() + " Rows Returned")).WriteExceptionToDB();
          return new DataLoad(_ds.Tables[0].Rows[0]);
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new DataLoad(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, loadID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad")).WriteExceptionToDB();
        return null;
      }
    }
    public override DataLoad GetDataLoad_CurrentCharLoad() {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_GetDataLoads_CurrentCharLoad");

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad_CurrentCharLoad - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no rosters to process
          //throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad_CurrentCharLoad - No Rows Returned");
          return new DataLoad();
        } else {
          //Okay, let's get to work - we have a table and roster to read
          return new DataLoad(_ds.Tables[0].Rows[0]);
        }

      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDataLoad_CurrentCharLoad")).WriteExceptionToDB();
        return null;
      }
    }

    public override int AddDataLoad(DataLoad objDataLoad) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        return (int)SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddDataLoad",
          SqlDataUtils.ValidateDbValue(objDataLoad.Roster_ID),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadType),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadStatus),
          SqlDataUtils.ValidateDbValue(objDataLoad.LastCharIdLoaded),
          SqlDataUtils.ValidateDbValue(objDataLoad.Load_StartedBy),
          SqlDataUtils.ValidateDbValue(objDataLoad.Load_Source),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadTime_Started),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadTime_Ended),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Adds),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Updates),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Skips),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Errors));
      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, objDataLoad.Load_ID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.AddDataLoad(RosterID: " + objDataLoad.Roster_ID.ToString() + ")")).WriteExceptionToDB();
        return 0;
      }
    }
    public override bool UpdateDataLoad(DataLoad objDataLoad) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_UpdateDataLoad",
          objDataLoad.Load_ID,
          SqlDataUtils.ValidateDbValue(objDataLoad.Roster_ID),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadType),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadStatus),
          SqlDataUtils.ValidateDbValue(objDataLoad.LastCharIdLoaded),
          SqlDataUtils.ValidateDbValue(objDataLoad.Load_StartedBy),
          SqlDataUtils.ValidateDbValue(objDataLoad.Load_Source),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadTime_Started),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadTime_Ended),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Adds),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Updates),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Skips),
          SqlDataUtils.ValidateDbValue(objDataLoad.LoadCount_Errors));
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, objDataLoad.Load_ID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.UpdateDataLoad")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteDataLoads() {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteDataLoads");
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, -1, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteDataLoads")).WriteExceptionToDB();
        return false;
      }
    }
    public override bool DeleteDataLoad(int loadID) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int update = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DeleteDataLoad",
          loadID);
        if (update > 0) {
          return true;
        } else {
          return false;
        }
      } catch (Exception e) {
        (new Business.Exceptions.GuildLoadError(e, loadID, "DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.DeleteDataLoad")).WriteExceptionToDB();
        return false;
      }
    }

    #endregion
    #region WowGuildRoster Supporting Methods - Errors
    internal override void AddError(DotNetWarcraft.WoWGuildRoster.Business.Exceptions.WoWGuildRosterException error) {
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }
        int _errorID = SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_AddError",
          SqlDataUtils.ValidateDbValue(error.LoadID),
          SqlDataUtils.ValidateDbValue(error.RosterID),
          SqlDataUtils.ValidateDbValue(error.CharID),
          SqlDataUtils.ValidateDbValue(error.CharStatID),
          SqlDataUtils.ValidateDbValue(error.ErrorTime),
          SqlDataUtils.ValidateDbValue(error.Exception.GetType().ToString()),
          SqlDataUtils.ValidateDbValue(error.AdditionalInfo),
          SqlDataUtils.ValidateDbValue("<b>" + error.Exception.Message + "</b> - " + error.Exception.StackTrace));
      } catch (Exception eExc) {
        throw eExc;
      }
    }
    #endregion

    #region WowGuildRoster Methods - Drop Down Lists
    internal enum DropDownValType {
      Classes = 1,
      Professions = 2,
      Reputations = 3
    }

    public override List<String> GetDropDowns_CharClasses() {
      return this.GetDropDownVals(DropDownValType.Classes);
    }
    public override List<String> GetDropDowns_CharProfessions() {
      return this.GetDropDownVals(DropDownValType.Professions);
    }
    public override List<String> GetDropDowns_Reputations() {
      return this.GetDropDownVals(DropDownValType.Reputations);
    }
    public override List<String> GetDropDowns_ReputationGroups(int charID) {
      List<String> _ddVals = new List<String>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }

        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + "DNW_WGR_DropDownVals_CharRepGroups", charID);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDropDowns_ReputationGroups(" + charID + ") - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no values to process
        } else {
          //Okay, let's get to work - we have a table and values to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            _ddVals.Add(_dr[0].ToString());
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _ddVals;
    }


    internal List<String> GetDropDownVals(DropDownValType ddType) {
      String _storedProc;
      List<String> _ddVals = new List<String>();
      try {
        if (this.ConnectionString == null) {
          throw new WoWGuildRoster.Business.Exceptions.NoConnectStringValueSpecified();
        }

        switch (ddType) {
          case DropDownValType.Classes:
            _storedProc = "DNW_WGR_DropDownVals_CharClasses";
            break;
          case DropDownValType.Professions:
            _storedProc = "DNW_WGR_DropDownVals_CharProfessions";
            break;
          case DropDownValType.Reputations:
            _storedProc = "DNW_WGR_DropDownVals_CharReputations";
            break;
          default:
            _storedProc = "DNW_WGR_DropDownVals_CharClasses";
            break;
        }
        DataSet _ds = SqlHelper.ExecuteDataset(ConnectionString, DatabaseOwner + ObjectQualifier + _storedProc);

        if (_ds.Tables.Count <= 0) {
          //No tables returned? - WTF?
          throw new ApplicationException("DotNetWarcraft.WoWGuildRoster.Data.SqlDataProvider.GetDropDownVals - No Tables Returned");
        } else if (_ds.Tables[0].Rows.Count <= 0) {
          //No rows were returned; no values to process
        } else {
          //Okay, let's get to work - we have a table and values to read

          foreach (DataRow _dr in _ds.Tables[0].Rows) {
            _ddVals.Add(_dr[0].ToString());
          }
        }

      } catch (Exception e) {
        (new Business.Exceptions.WGRGeneralError(e)).WriteExceptionToDB();
      }

      return _ddVals;
    }
    #endregion
  }
}
